package activity;


import org.shmsa.hst4a.R;

import system.AppInfo;
import system.AppInfo.AppItem;
import system.UserInfo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LoadApps extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.grid);
        
        GridView gridview = (GridView)findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //Toast.makeText(LoadApps.this,  parent+","+v+","+position+","+id, Toast.LENGTH_SHORT).show();
            	AppItem ai=AppInfo.apps.get(position);
            	
            	Intent intent=new Intent();
            	String appClassName=AppInfo.package_prix+"."+AppInfo.activity_prix+id;
            	try {
            		//if(!ai.isSms())
					//intent.setClass(LoadApps.this, Class.forName(appClassName));
            		if(ai.isSms()&&(!UserInfo.smsed))
            		{
            			intent.setClass(LoadApps.this, SmsCheck.class);
            			intent.putExtra("goto",appClassName);
            		}
            		else
            			intent.setClass(LoadApps.this, Class.forName(appClassName));
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
            	startActivity(intent);
            }
        });
        
       
        
        
    }
    
    
     class ImageAdapter extends BaseAdapter {

   	 private Context mContext;

   	    public ImageAdapter(Context c) {
   	        mContext = c;
   	    }

		@Override
		public int getCount() {
			return AppInfo.apps.size();// mThumbIds.length;

		}

		@Override
		public Object getItem(int position) {
			//return null;
			return AppInfo.apps.get(position);
		}

		@Override
		public long getItemId(int position) {
			return AppInfo.apps.get(position).getId();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			//LinearLayout v=(LinearLayout)LayoutInflater.from(mContext).inflate(R.layout.grid_item, parent);
			LinearLayout v=null;
			 if (convertView == null) {  // if it's not recycled, initialize some attributes
				 v=(LinearLayout)LayoutInflater.from(mContext).inflate(R.layout.grid_item, null);
				 //v=(LinearLayout)View.inflate(mContext, R.layout.grid_item, null);
		        } else {
		            v = (LinearLayout) convertView;
		        }
			 ImageView iv=(ImageView)v.findViewById(R.id.gridimg);
			 TextView tv=(TextView)v.findViewById(R.id.gridtit);
			 			
			 try {
				iv.setImageResource(R.drawable.class.getField(AppInfo.icon_prix+getItemId(position)).getInt(null));
				tv.setText(R.string.class.getField(AppInfo.title_prix+getItemId(position)).getInt(null));
			}  catch (Exception e) {
				e.printStackTrace();
			}
			
			
		    return v;

		}
	
		
		

   	
   }
}